package com.fyzk.activity.lottery.shuzicai.klpk;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;


public class KLPKPopupwindow extends PopupWindow implements View.OnTouchListener{

	private Context context;
	private List<KLPKPopupItemBean> items1 = new ArrayList<>();
	private List<KLPKPopupItemBean> items2 = new ArrayList<>();
	private View view;
	private LinearLayout common_ll;
	private GridView gv;
	private LinearLayout dan_ll;
	private GridView gv_dan;
	private int checkItem = 0;
	private KLPKPopuponItemListener listener;
	private KLPKPopupAdapter adapter;
	private KLPKPopupAdapter adapter_dan;

	private int ordinaryCount = 11;		// 普通玩法数量

	public KLPKPopupwindow(Context context, int w, int h, List<KLPKPopupItemBean> items, int ordinaryCount){
		this(context, w, h, items, ordinaryCount, true);
	}

	public KLPKPopupwindow(Context context, int w, int h, List<KLPKPopupItemBean> items, int ordinaryCount, boolean isTouchOutside){
		super(w, h);
		this.context = context;
		this.ordinaryCount = ordinaryCount;
		View view_pop = LayoutInflater.from(context).inflate(R.layout.klpkpopupwindow, null);
		this.view = view_pop;
		this.view.setOnTouchListener(this);
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				if (i < this.ordinaryCount) {
					this.items1.add(items.get(i));
				} else {
					this.items2.add(items.get(i));
				}
			}
		}
		initView();
		this.setContentView(view);
		this.setWidth(w);
		this.setHeight(h);
		this.setFocusable(isTouchOutside);
		this.setOutsideTouchable(isTouchOutside);
		// 刷新状态
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.showPopupAnimation);
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		this.dismiss();
		return true;
	}

	private void initView(){
		common_ll = view.findViewById(R.id.klpk_popu_common_ll);
		gv = view.findViewById(R.id.klpk_popu_gv);
		adapter = new KLPKPopupAdapter(items1, 0);
		gv.setEnabled(false);
		gv.setAdapter(adapter);

		dan_ll = view.findViewById(R.id.klpk_popu_dan_ll);
		gv_dan = view.findViewById(R.id.klpk_popu_gv_dan);
		adapter_dan = new KLPKPopupAdapter(items2, ordinaryCount);
		gv_dan.setEnabled(false);
		gv_dan.setAdapter(adapter_dan);
		if (items2.size() == 0) {
			common_ll.setVisibility(View.GONE);
			dan_ll.setVisibility(View.GONE);
			gv_dan.setVisibility(View.GONE);
		}
	}
	
	
	/**
     * 显示popupWindow
     * 
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
        	if (null != listener) {
				listener.popupShow();
			}
        	super.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }
    
    private void popupDismiss(){
    	this.dismiss();
    }
    
    @Override
    public void dismiss() {
    	if (null != listener) {
			listener.popupDismiss();
		}
    	super.dismiss();
    }
    
    public void setCheckItem(int checkItem){
    	this.checkItem = checkItem;
    }
    
    public int getCheckItem(){
    	return this.checkItem;
    }
    
    //===== 接口	==========
    public interface KLPKPopuponItemListener {
    	void onItem(int position);
    	void popupShow();
    	void popupDismiss();
    }
    
    public void setHm_PopupOnItemListener(KLPKPopuponItemListener listener){
    	this.listener = listener;
    }
    
    public void setItems(List<KLPKPopupItemBean> items){
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				if (i < ordinaryCount) {
					this.items1.add(items.get(i));
				} else {
					this.items2.add(items.get(i));
				}
			}
		}
    	adapter.setItems(items1);
    	adapter_dan.setItems(items2);
    }
    
    //==========================================================================
	class KLPKPopupAdapter extends BaseAdapter{
		private List<KLPKPopupItemBean> items = new ArrayList<>();
		private int startIndext = 0; 	// 起始位置 0：普通玩法，其它：胆拖
		
		public KLPKPopupAdapter(List<KLPKPopupItemBean> items, int startIndext){
			this.items = items;
			this.startIndext = startIndext;
		}

		@Override
		public int getCount() {
			
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			KLPKPopupItemBean item = items.get(position);
			convertView = LayoutInflater.from(context).inflate(R.layout.klpk_popupwindow_item, null);
			LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.klpk_popup_item_ll);
			LinearLayout prize_ll = (LinearLayout) convertView.findViewById(R.id.klpk_popup_item_prize_ll);
			TextView text = (TextView) convertView.findViewById(R.id.klpk_popup_item_tv);
			TextView text2 = (TextView) convertView.findViewById(R.id.klpk_popup_item_tv2);
			TextView text3 = (TextView) convertView.findViewById(R.id.klpk_popup_item_tv3);

			text.setText(item.str1);
			if (!item.str2.equals("") && !item.str3.equals("")) {
				prize_ll.setVisibility(View.VISIBLE);
				text2.setText(item.str2);
				text3.setText(item.str3);
			} else {
				prize_ll.setVisibility(View.GONE);
			}

			if (checkItem == position+startIndext) {
				text.setTextColor(context.getResources().getColor(R.color.klpk_color_a3400c));
				ll.setBackground(context.getResources().getDrawable(R.drawable.klpk_playchoose_click));
			}else{
				text.setTextColor(context.getResources().getColor(R.color.klpk_color_9f9f9f));
				ll.setBackground(context.getResources().getDrawable(R.drawable.klpk_playchoose_normal));
			}
			
			ll.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (checkItem != position+startIndext && null != listener) {
						checkItem = position+startIndext;
						listener.onItem(position+startIndext);
					}

					notifyDataSetChanged();
					popupDismiss();
				}
			});
			
			return convertView;
		}
		public void setItems(List<KLPKPopupItemBean> items){
			this.items = items;
			this.notifyDataSetChanged();
		}
		
	}

}
